<template>
  <el-dialog
    top="5vh"
    title="下载"
    :visible.sync="open"
    width="70%"
    destroy-on-close
  >
    <el-table v-loading="loading" :data="tableData">
      <el-table-column label="文件名" align="center" prop="name" />
      <el-table-column label="类型" align="center" prop="fileType" />
      <el-table-column label="大小" align="center" prop="fileSize" />
      <el-table-column label="操作" align="right" width="260px">
        <template slot-scope="scope">
          <el-button
            v-show="isPreview(scope.row.fileType)"
            size="mini"
            type="success"
            icon="el-icon-view"
            @click="handlePreview(scope.row.path)"
          >预览
          </el-button>
          <el-button
            size="mini"
            type="primary"
            icon="el-icon-download"
            @click="handleDownload(scope.row)"
          >下载
          </el-button>
          <el-button
            size="mini"
            type="danger"
            icon="el-icon-delete"
            @click="handleDelete(scope.row.id)"
          >删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-dialog>
</template>

<script>
import { baseURL } from '@/utils/request'
import { getToken } from '@/utils/auth'
import { saveFileToLink } from '@/utils/web-downloadfile'

export default {
  name: 'Index',
  data() {
    return {
      // 遮罩层
      loading: true,
      open: false,
      queryParams: {
        relevanceId: '',
        fileCategory: ''
      },
      tableData: []
    }
  },
  methods: {
    isPreview(fileType) {
      if (fileType) {
        return ['pdf', 'png', 'peg', 'jpg', 'jpeg', 'bmp', 'gif', 'webp', 'psd', 'svg', 'tiff'].indexOf(fileType.toLowerCase()) !== -1
      } else {
        return false
      }
    },
    showDialog(relevanceId, fileCategory) {
      this.open = true
      this.queryParams.relevanceId = relevanceId
      this.queryParams.fileCategory = fileCategory
      this.getPathByRelevanceId()
    },
    getPathByRelevanceId() {
      this.loading = true
      this.$http.post('/sysPath/getPathByRelevanceId', this.queryParams).then(response => {
        this.tableData = response.data
        this.$set(this, 'tableData', response.data)
        this.loading = false
      })
    },
    handlePreview(path) {
      window.open(baseURL + path + '?token=' + getToken())
    },
    handleDownload(row) {
      // const fileType = row.name.substr(row.name.lastIndexOf('.') + 1)
      const fileType = row.fileType
      if (this.isPreview(fileType)) {
        const fileName = row.path.split('/').pop().split('.')[0]
        const link = baseURL + row.path + '?token=' + getToken()
        saveFileToLink(link, fileName, fileType)
      } else {
        this.download(row.path)
      }
    },
    /** 删除按钮操作 */
    handleDelete(id) {
      this.$confirm('是否确认删除?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        return this.$http.post('/sysPath/deletePathById', { id: id })
      }).then(() => {
        this.msgSuccess('删除成功')
        this.getPathByRelevanceId()
      }).catch(function() {
      })
    }
  }
}
</script>

<style scoped>
  ::v-deep .el-dialog__header {
    padding: 15px;
    border-bottom: 2px solid #ff841c;
  }
</style>
